package es.cea.agendatelefonica;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Borrado
 */
@WebServlet("/Borrado")
public class Borrado extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
    public Borrado() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String nombreString = request.getParameter("nombre");
		

		 List<ContactoTel> contactos = (List)request.getSession().getServletContext().getAttribute("contactos");
		 ContactoTel aux = null;
		 System.out.println(contactos);
		 for(ContactoTel contactoTel:contactos){
			 
			 if(contactoTel.getNombre().equals(nombreString)){
				// System.out.println(contactoTel+" borrando....");
				 	aux = contactoTel;	
			 }
			 
		 }
		 
		 contactos.remove(aux);
		 System.out.println((List)request.getSession().getServletContext().getAttribute("contactos"));
				//publicar el modelo
				request.setAttribute("contactos", aux);
				//seleccionar la vista
				request.getRequestDispatcher("confirmaBorrado.jsp")
				.forward(request, response);
				
		 
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
